package com.iraeta.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.order.GiftOrder;
import com.iraeta.entity.resulttype.GiftOrderRT;
import com.iraeta.entity.resulttype.GiftOrderTs;
import com.iraeta.entity.type.GiftOrderStatus;
import com.iraeta.service.order.IGiftOrderService;
import com.iraeta.uitl.Util;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController(value = "GiftOrderController")
@RequestMapping("gift/order")
public class GiftOrderController extends BaseController {
    @Resource
    IGiftOrderService giftOrderService;

    /*正常订单查询*/
    @RequestMapping("listgiftorder")
    public String listGiftOrder(@RequestBody JSONObject jo){
        String orderno = jo.getString("orderno");
        String giftName = jo.getString("giftName");
        this.pageSet(jo);
        List<GiftOrderRT> orderRTList = giftOrderService.listSelfGiftOrder(this.getUserId(), orderno, giftName);
        orderRTList.forEach(x->{
            x.setStatusname(GiftOrderStatus.getStatusName(x.getStatusId()));
        });
        PageInfo<GiftOrderRT> orderRTPageInfo = this.getPageInfo(orderRTList);
        return this.success(orderRTPageInfo);
    }

    /*特殊订单查询*/
    @RequestMapping("listtsgiftorder")
    public String listTsGiftOrder(@RequestBody JSONObject jo){
        String orderno = jo.getString("orderno");
        this.pageSet(jo);
        List<GiftOrderTs> orderTsList = giftOrderService.listSelfTsGiftOrder(this.getUserId(), orderno);
        orderTsList.forEach(x->{
            x.setStatusname(GiftOrderStatus.getStatusName(x.getStatusId()));
        });
        PageInfo<GiftOrderTs> orderTsPageInfo = this.getPageInfo(orderTsList);
        return this.success(orderTsPageInfo);
    }

    @RequestMapping("addgiftorder")
    public String addGiftOrder(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        GiftOrder giftOrder = id == 0 ? new GiftOrder() : giftOrderService.getById(id);
        this.mapJsonToObj(paramJb, giftOrder);
        if(id == 0){
            giftOrder.setOrderNo(Util.getBusinessNo());
            giftOrder.setCreateTime(new Date());
            giftOrder.setCreateUserid(this.getUserId());
            giftOrder.setStatusId(GiftOrderStatus.add);
            if(giftOrder.getGiftId() == null) giftOrder.setGiftId(0);
            if(giftOrder.getModelFileid() == null) giftOrder.setModelFileid(0);
            giftOrderService.save(giftOrder);
        }else {
            giftOrderService.updateById(giftOrder);
        }
        return this.success();
    }

    /*正常订单*/
    @RequestMapping("getgiftorder")
    public String getGiftOrder(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        GiftOrderRT giftOrderRT = giftOrderService.getGiftOrder(id);
        return this.success(giftOrderRT);
    }

    /*特殊订单*/
    @RequestMapping("gettsgiftorder")
    public String getTsGiftOrder(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        GiftOrderTs giftOrderTs = giftOrderService.getTsGiftOrder(id);
        return this.success(giftOrderTs);
    }

    @RequestMapping("copygiftorder")
    public String copyGiftOrder(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        GiftOrder giftOrder = giftOrderService.getById(id);
        giftOrder.setOrderNo(Util.getBusinessNo());
        giftOrder.setCreateTime(new Date());
        giftOrder.setCreateUserid(this.getUserId());
        giftOrder.setStatusId(GiftOrderStatus.add);
        giftOrder.setId(null);
        giftOrderService.save(giftOrder);
        return this.success();
    }

    @RequestMapping("submitgiftorder")
    public String submitGiftOrder(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        giftOrderService.submitGiftOrder(id);
        return this.success();
    }

    @RequestMapping("deletegiftorder")
    public String deleteGiftOrder(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        giftOrderService.removeById(id);
        return this.success();
    }
}
